var editUserUrl = null;

var addUser = function () {
    editUserUrl = ctx + 'sys/user_add';
    $('#form_user_edit').form('clear');
    $('#form_user_edit select[name="user.state"]').val(1);
    $('#dl_user_edit').dialog('open');
}

var updateUser = function () {
    var selected = $('#dg_user').datagrid('getSelected');
    if (!selected) {
        $.messager.alert('系统提示', "请选择您要修改的用户");
        return;
    }
    $('#form_user_edit input[name="user.id"]').val(selected.id);
    $('#form_user_edit input[name="user.nick"]').val(selected.nick);
    $('#form_user_edit input[name="user.account"]').val(selected.account);
    $('#form_user_edit input[name="user.pwd"]').val(selected.pwd);
    $('#form_user_edit select[name="user.state"]').val(selected.state);
    editUserUrl = ctx + '/sys/user_update';
    $('#dl_user_edit').dialog('open');
}

var closeUserEdit = function () {
    $('#dl_user_edit').dialog('close');
}

var submitUser = function () {
    $('#form_user_edit').form('submit', {
        url: editUserUrl,
        success: function (data) {
            data = eval('(' + data + ')');
            closeUserEdit();
            $('#dg_user').datagrid('reload');
            if (data && data.msg) {
                $.messager.alert('系统提示', data.msg);
            }
        }
    });
}

var removeUser = function () {
    var url = ctx + "sys/user_delete";
    var selected = $('#dg_user').datagrid('getSelected');
    if (!selected) {
        $.messager.alert('系统提示', "请选择您要修改的用户");
        return;
    }
    $.messager.confirm("警告", "您确定要删除用户“" + selected.nick + "”吗？", function (r) {
        if (!r) {
            return;
        }
        $.post(url, {'user.id': selected.id}, function (data) {
            $('#dg_user').datagrid('reload');
            if (data && data.msg) {
                $.messager.alert('系统提示', data.msg);
            }
        });
    })
}

var searchUser = function () {
    $('#dg_user').datagrid('load', {
        'user.id': $('#searUserParams input[name="user.id"]').val(),
        'user.nick': $('#searUserParams input[name="user.nick"]').val(),
        'user.account': $('#searUserParams input[name="user.account"]').val()
    });
}